package com.blockmeta.bbs.immodule.activity;

import android.view.View;

import com.blockmeta.bbs.baselibrary.base.adapter.BaseAdapter;
import com.blockmeta.bbs.businesslibrary.arouter.RouterHelper;
import com.blockmeta.bbs.businesslibrary.arouter.RouterPath;
import com.blockmeta.bbs.businesslibrary.net.pojo.BaseImResponse;
import com.blockmeta.bbs.businesslibrary.net.retrofit.RetrofitFactory;
import com.blockmeta.bbs.businesslibrary.net.rxjava.RxUtil;
import com.blockmeta.bbs.businesslibrary.util.AccountHelper;
import com.blockmeta.bbs.immodule.R;
import com.blockmeta.bbs.immodule.activity.base.BaseIMSearchActivity;
import com.blockmeta.bbs.immodule.adapter.RecommandUserAdapter;
import com.blockmeta.bbs.immodule.bean.databean.UserBean;
import com.blockmeta.bbs.immodule.corebean.Impl.IMApplicationContext;
import com.blockmeta.bbs.immodule.corebean.IUserDomainService;
import com.blockmeta.bbs.immodule.net.api.IMNetApi;
import com.blockmeta.bbs.immodule.widget.IMAddFriendMsg;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.RequestBody;

public class SearchFriendActivity extends BaseIMSearchActivity implements BaseQuickAdapter.OnItemClickListener {
    private RecommandUserAdapter mAdapter;
    private MediaType mMediaType = MediaType.parse("application/json; charset=utf-8");
    private HashMap<String,Object> mParams= new HashMap<>();
    private IUserDomainService mIUserRepo;
    @Override
    protected BaseAdapter getAdapter(){
        if (mAdapter==null){
            mAdapter = new RecommandUserAdapter(R.layout.item_recommanduser,null);
            mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
                @Override
                public void onItemChildClick(BaseQuickAdapter adapter, View view, final int position) {
                    int i = view.getId();
                    if (i == R.id.btn_addfriend) {
                        final UserBean bean = mAdapter.getData().get(position);
                        if (bean != null) {
                            IMAddFriendMsg.OnRequestSuccessListiener requestSuccessListiener = new IMAddFriendMsg.OnRequestSuccessListiener() {
                                @Override
                                public void requestSuccess() {
                                    bean.setHasBeenSentFriendRequest(true);
                                    mAdapter.notifyItemChanged(position);
                                }
                            };
                            IMAddFriendMsg imPopAddFriendMsg = new IMAddFriendMsg(mContext, bean.getId() + "",requestSuccessListiener);
                            imPopAddFriendMsg.show();
                        }

                    } else {
                    }
                }
            });
            mAdapter.setOnItemClickListener(this);
        }
        return mAdapter;
    }

    @Override
    protected void initialize() {
        super.initialize();
        setTitleText("搜索用户");
        mEdtSearchInput.setHint("搜索用户名/用户ID");
        mIUserRepo = IMApplicationContext.getUserDomainService();
    }

    @Override
    protected void searchOnNet(String content) {
        mParams.put("searchContent", content);
        mParams.put("queryType","1");
        RequestBody requestBody = RequestBody.create(mMediaType,new Gson().toJson(mParams));

        RetrofitFactory.getRetrofit().create(IMNetApi.SearchIMUser.class)
                .searchFriend(requestBody)
                .compose(this.<BaseImResponse<List<UserBean>>>bindToLifecycle())
                .compose(RxUtil.<BaseImResponse<List<UserBean>>>rxSchedulerHelper())
                .compose(RxUtil.<List<UserBean>>commonResult())
                .subscribe(new IMSubscriber<List<UserBean>>(mContext) {
                    @Override
                    public void onNext(List<UserBean> list) {

                        for (UserBean userBean : list) {
                            if (mIUserRepo==null){
                                break;
                            }
                            userBean.setFriend(mIUserRepo.isFriend(AccountHelper.getUidInt(),userBean.getId()));
                            userBean.setGroupId(-1);
                        }
                        mAdapter.setNewData(list);
                    }
                });

    }

    @Override
    protected View setEmptyView() {
        return getLayoutInflater().inflate(R.layout.empty_search_for_im,null);
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        UserBean data= (UserBean) adapter.getData().get(position);
        RouterHelper.startActivity(mContext, RouterPath.PERSONHOME,String.valueOf(data.getId()));
    }
}
